package com.example.yslapplication.slice;

import com.example.yslapplication.ResourceTable;
import com.example.yslapplication.utils.DeviceUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice implements IAbilityContinuation {

    private TextField textField;
    private String data;
    private Button btn;
    private Button btn_qh;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //加载页面布局
        super.setUIContent(ResourceTable.Layout_ability_main_my);

        textField = (TextField) findComponentById(ResourceTable.Id_et_text);

        //迁移完成后，把数据恢复到控件上
        textField.setText(data);

        btn = (Button) findComponentById(ResourceTable.Id_btn_qy);
        //点击事件
        btn.setClickedListener(component -> {
            List<String> devices = DeviceUtil.getDevices();
            if (devices != null && !devices.isEmpty()){
//                continueAbility(devices.get(0));//支持迁移,不可迁回
                continueAbilityReversibly(devices.get(0));//改为可撤回的迁移
            }
        });

        btn_qh = (Button) findComponentById(ResourceTable.Id_btn_qh);
        btn_qh.setClickedListener(component -> {
            reverseContinueAbility();
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onStartContinuation() {//开始流转

        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        intentParams.setParam("data", textField.getText());
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        data = intentParams.getParam("data").toString();
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {//完成流转
//        terminateAbility();//将迁移前的页面关闭；但是可迁回时不能关闭
    }

    @Override
    public void onRemoteTerminated() {

    }
}
